<?php

class Weergave {
    private static $_dit = null;
    
    private $_layout = 1;
    private $_weergave = [];
    private $_template = 'frontend';
    private $_head = ['favicon'=>'',
                      'titel'=>'',
                      'metakeys'=>'',
                      'metadesc'=>''];
    private $_soorten = ['js','css'];
    private $_koppelingen = [['js','frameworks/jq/jquery.js'],
                             ['css','frameworks/jq/jquery-ui.min.css'],
                             ['js','frameworks/jq/jquery-ui.min.js'],
                             ['css','frameworks/bootstrap/css/bootstrap.min.css'],
                             ['js','frameworks/bootstrap/js/bootstrap.min.js'],
                             ['js','frameworks/validation/dist/jquery.validate.min.js'],
                             ['js','frameworks/validation/dist/additional-methods.min.js'],
                             ['js','frameworks/_cms/cms_base_pack.js'],
                             ['js','frameworks/_cms/validation_extend.js']];
    
    public function __construct() {
        $this->_head['titel'] = \instelling::GET('website.head.titel');
        $this->_head['favicon'] = \instelling::GET('website.head.favicon');
        $this->_head['metakeys'] = \instelling::GET('website.head.metakeys');
        $this->_head['metadesc'] = \instelling::GET('website.head.metadesc');
    }
    
    public static function SETKOPPELING($soort='',$pad='') {
        if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        $soort = strtolower(trim($soort));
        $pad = trim($pad);
        if (!empty($soort) && in_array($soort,self::$_dit->_soorten) && !empty($pad)) {
            array_push(self::$_dit->_koppelingen,[$soort,$pad]);
        }
    }
    
    public static function SETLAYOUT($layout=1) {
         if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        $layout = trim($layout);
        self::$_dit->_layout = $layout;
    }
    
    public static function SETHEADITEM($sleutel='',$waarde='') {
         if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        $sleutel = strtolower(trim($sleutel));
        if (!empty($sleutel) && array_key_exists($sleutel,self::$_dit->_head)) {
            if ($sleutel == 'titel') {
                self::$_dit->_head[$sleutel] .= ': '.$waarde;
            } else {
                self::$_dit->_head[$sleutel] .= ','.$waarde;
            }
        }
    }
    
    public static function SETWEERGAVE($sleutel='', $inhoud='', $vervang=false) {
        if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        $sleutel = strtolower(trim($sleutel));
        $inhoud = trim($inhoud);
        if (!empty($sleutel)) {
            if (!isset(self::$_dit->_weergave[$sleutel])) {
                self::$_dit->_weergave[$sleutel] = '';
            }
            if ($vervang) {
                self::$_dit->_weergave[$sleutel] = '';
            }
            self::$_dit->_weergave[$sleutel] .= $inhoud;
        }
    }
    
    public static function SETTEMPLATE($template='frontend') { 
        if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        
        $template = strtolower(trim($template));
        self::$_dit->_template = $template == 'backend'?'backend':'frontend';
    }
    
    public function getWeergave($inhoud = '') {
        $inhoud = strtolower(trim($inhoud));
        if (array_key_exists($inhoud,$this->_weergave)) {
            return $this->_weergave[$inhoud];
        }
        else {
            return '';
        }
    }
    
    private function _prepHead() {
        $webRoot = \Instelling::GET('website.root');
        
        $html = '';        
        foreach(self::$_dit->_head as $sleutel => $waarde) {
            if (!empty($waarde)) {
                if ($sleutel == 'favicon') {
                    $html .= '<link rel="shortcut icon" type="image/icon" href="'.$waarde.'">';
                }
                elseif ($sleutel == 'titel') {
                    $html .= '<title>'.$waarde.'</title>';
                }
                elseif ($sleutel == 'metakeys') {
                    $html .= '<meta name="keywords" content="'.$waarde.'">';
                }
                elseif ($sleutel == 'metadesc') {
                    $html .= '<meta name="description" content="'.$waarde.'">';
                }
                elseif ($sleutel == 'redirect') {
                    $delay = \Instelling::GET('sessie.redirectwait');
                    $url = \Instelling::GET('website.root');
                    $html .= '<meta http-equiv="refresh" content="'.$delay.';URL='.$url.'" />';
                }
            }
        }
        foreach (self::$_dit->_koppelingen as $koppeling) {
            $soort = $koppeling[0];
            $pad = $webRoot.$koppeling[1];
            if ($soort == 'js') {
                $html .= '<script src="'.$pad.'"></script>';
            }
            elseif ($soort == 'css') {
                $html .= '<link rel="stylesheet" href="'.$pad.'">';
            }
        }
        $this->_weergave['head'] = $html;
    }
    
    private function _prepLogo() {
        $webRoot = \Instelling::GET('website.root');
        
        $naam = \Instelling::GET('website.naam');
        $slogan = \Instelling::GET('website.slogan');
        $logo = \Instelling::GET('website.logo');
        
        $html = '<a href="'.$webRoot.'" class="navbar-brand">';
        if (empty($logo)) {
            $html .= $naam.' <small>'.$slogan.'</small>';
        }
        else {
            $html .= '<img src="'.$webRoot.$logo.'">';
        }
        $html .= '</a>';
        
        $this->_weergave['logo'] = $html;
    }
    
    private function _prepAdminBalk() {
        $adminBalk = new \WidgetAdminBalk\Controller\Index();
        $adminBalk->Index();
    }
    
    private function _prepRegios() {
        // todo: koppelen aan groep
        try {
            $dbSql = "SELECT id,regio FROM regio";
            $dbStmt = \Db::GET()->prepare($dbSql);
            $dbStmt->execute();
            if ($dbStmt->rowCount() > 0) {
                $dbSql = "SELECT wr.id, wr.instelling, w.pad
                          FROM widgets w JOIN widgetregio wr ON w.id=wr.idregio
                          WHERE idregio=:idregio AND wr.publiek = 1
                          ORDER BY wr.volgnummer";
                $dbStmtW = \Db::GET()->prepare($dbSql);
                while ($dbRij = $dbStmt->fetch(PDO::FETCH_ASSOC)) {
                    $dbStmtW->execute([':idregio'=>$dbRij['id']]);
                    if ($dbStmtW->rowCount() > 0) {
                        while($dbWidget = $dbStmtW->fetch(PDO::FETCH_ASSOC)) {
                            $widget = new $dbWidget['pad']();
                            $widget->index($dbRij['regio']);
                        }
                    }
                }
            }
        } catch (PDOException $ex) {

        }
    }
    
    private function _prepDashboardBalk() {
        $dashboardBalk = new \WidgetDashboard\Controller\Index();
        $dashboardBalk->Index();
    }
    
    private function _prep() {
        $this->_prepAdminBalk();
        $this->_prepHead();
        $this->_prepLogo();
        $this->_prepRegios(); 
        
        if ($this->_template == 'backend') {
            $this->_prepDashboardBalk();
        }
    }
    
    public function toon() {
        $this->_prep();
        $template = \Instelling::GET('website.template.'.$this->_template);
        require_once(implode(DIRECTORY_SEPARATOR,[ROOT,'templates',$template,'template.php']));
    }
    
    public static function GENEREER() {
         if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        
        self::$_dit->toon();
    }
    
    public function get($sleutel='') {
        $sleutel = strtolower(trim($sleutel));
        if (array_key_exists($sleutel,$this->_weergave)) {
            return $this->_weergave[$sleutel];
        }
        else {
            return '';
        }
    }
    
    public static function REDIRECTHOME() {
        if (!self::$_dit instanceof self) {
            self::$_dit = new self();
        }
        self::$_dit->_head['redirect'] = 1;
    }
}